package GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFrame;

import common.ChatIF;
import common.Perror;
import controllers.*;
import client.ChatClient;
import entities.*;
import GUI.lecturerGUI.LecturerCheckGrade;
import GUI.lecturerGUI.LecturerDurationTimeRequest;
import GUI.lecturerGUI.LecturerExam;
import GUI.lecturerGUI.LecturerGenerateExamCode;
import GUI.lecturerGUI.LecturerReportFile;
import GUI.lecturerGUI.LecturerReports;
import GUI.lecturerGUI.LecturerWindow;
import GUI.loginGUI.LoginGUI;
import MsgPackage.*;
 
 
 public class MainGUI extends JFrame implements ActionListener, ChatIF {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final public static Dimension btnsDimesion = new Dimension(115, 30);
	final public static Font myfont = new Font("Microsoft New Tai Lue",
			Font.BOLD, 18);
	final public static Font PanelFont = new Font("Microsoft New Tai Lue",
			Font.BOLD, 12);
	final public static Color prcolor = new Color(0, 0, 225);
	final public static int setX = 500, setY = 500;
	
	public static int mainX = 170, mainY = 150;
   
	public static Login lgnUsr;
	public static MainGUI mainframe;
	private LoginGUI loginUserGUI;
	private ChatClient client;
	
	private LecturerController 	lecturerCtrl;
	private StudentController 	studentCtrl;

	public static void main(String[] args) {

		MainGUI turnOn = new MainGUI();
		turnOn.start();
	}

	public MainGUI() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		mainX = ((int)screenSize.getWidth()/2-190);
		mainY = ((int)screenSize.getHeight()/2-125);
		mainframe = this;
		mainframe.setUndecorated(false);
		mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainframe.setLocation(mainX, mainY);
		mainframe.setSize(380, 249);
		mainframe.setVisible(true);
	};

	public void start() {
		loginUserGUI = new LoginGUI(this);
		setContentPane(loginUserGUI);

	}

	public void handleLoginGUI() {

		lgnUsr = new Login(loginUserGUI.getHost(), loginUserGUI.getPort(),
				loginUserGUI.getId(), loginUserGUI.getPass());
		LoginPack lgnmsg = new LoginPack(lgnUsr);
		try {
			client = new ChatClient(lgnUsr.getHost(), lgnUsr.getPort(), this);
			client.handleMessageFromClientUI(lgnmsg);
			lgnmsg = (LoginPack) client.getMessage();
			lgnUsr=lgnmsg.getUsr();
			handleLogin(lgnUsr.getLoginPermissionLevel());
		} catch (IOException exception) {
			common.Perror.pError("Can't setup connection!");
		}

	}
/**
 * user logout from the system 
 */
	public void logOut(){
	
		LogoutPack lgtmsg = new LogoutPack(lgnUsr);
		client.handleMessageFromClientUI(lgtmsg);
		try {
			client.closeConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}
		lgnUsr=null;
	}

/**
 * 
 * @param operation handle Login operation
 */
	public void handleLogin(int operation) {

		switch (operation) {
		case Login.ERROR:
			Perror.pError("User and/or Password is incorrect!");
			//loginUserGUI.cleanLoginGUI();
			break;
		case Login.UserAlreadyLogged:
			Perror.pError("User is already logged in to the system.\n Please contact your system administrator.");
			break;
		case Login.PRINCIPAL_PRMSSN:
			//principalGUI = new PrincipalWindow(this);
			loginUserGUI.cleanLoginGUI();

			break;
		case Login.LECTURER_PRMSSN:
			loginUserGUI.cleanLoginGUI();
			lecturerCtrl = new LecturerController(client, mainframe);
			
			break;
		case Login.STUDENT_PRMSSN:
			loginUserGUI.cleanLoginGUI();
			studentCtrl = new StudentController(client, mainframe);
			break;
		case Login.SALESMAN_PRMSSN:
			loginUserGUI.cleanLoginGUI();
			studentCtrl = new StudentController(client, mainframe);
			break;
		}
	}
// log in to the system
	public void returnToLogin() {
		logOut();
		mainframe.setSize(380, 249);
		mainframe.setContentPane(loginUserGUI);
		loginUserGUI.enableButtons();

	}

		
	@Override
	public void actionPerformed(ActionEvent e) {
		

	}

	public static void handleprincipalGUI(int i) {
	

	}

	public void display(Object message) {

		Perror.pError(message.toString());
	}

}
